package com.bjpowernode.p2p.comtroller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.app.vo.PageInfo;
import com.bjpowernode.p2p.constant.Loan;
import com.bjpowernode.p2p.constant.Page;
import com.bjpowernode.p2p.constant.WebPath;
import com.bjpowernode.p2p.entity.BidInfo;
import com.bjpowernode.p2p.entity.LoanInfo;
import com.bjpowernode.p2p.entity.UserRanking;
import com.bjpowernode.p2p.service.loan.LoanInfoService;
import com.bjpowernode.p2p.service.loan.LoanListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

@Controller
public class LoanController {


    @Autowired
    PageInfo<LoanInfo> pageInfo;

    @Reference(interfaceClass = LoanListService.class, version = "1.0", check = false)
    LoanListService loanListService;

    @Reference(interfaceClass = LoanInfoService.class,version = "1.0",check = false)
    LoanInfoService loanInfoService;


    @RequestMapping(WebPath.LOAN_PATH)
    public String loanList(Model model,
                           @RequestParam(defaultValue = "1") int productType,
                           @RequestParam(defaultValue = "1") int currentPage) {

        int countLoanQueryByproductType = loanListService.countLoanQueryByproductType(productType);
        HashMap<String,Object> paramMap=new HashMap<String,Object>();

        paramMap.put(Loan.PRODUCT_TYPE,productType);
        paramMap.put(Page.CURRENT_PAGE,currentPage);
        paramMap.put(Page.PAGE_SIZE,pageInfo.getPageSize());
        List<LoanInfo> loanInfos = loanListService.queryByproductType(paramMap);
        pageInfo.execute(countLoanQueryByproductType,currentPage, loanInfos);
        model.addAttribute(Page.PAGEHELP,pageInfo);
        model.addAttribute(Loan.PRODUCT_TYPE,productType);
        //排行榜
        List<UserRanking> userRankingList = loanListService.queryUserRankingService();
        model.addAttribute(Loan.USERRANKINGLIST,userRankingList);
        return WebPath.LOAN_RETURN;
    }

    @RequestMapping(WebPath.LOAN_INFO_PATH)
    public  String  loanInfo(Model model,
                             @RequestParam(defaultValue = "0")int id){
        LoanInfo loanInfo = loanInfoService.queryById(id);
        List<BidInfo> bidInfos = loanInfoService.queryByUser(id);
        model.addAttribute("loanInfo", loanInfo);
        model.addAttribute("bidInfos", bidInfos);
        return WebPath.LOAN_INFO_RETURN;
    }



}
